<?php
/*
	批量生成防伪码
	参数
		$security_code		防伪码FW53440264246609859500000070077146891

	返回值
		布尔值，真表示有效，假表示无效
*/
defined('BY_JYA') or exit('error');
list($security_code)=$arguments;
$code=pdo_get('jy_goodserpfw_code',array('code'=>$security_code,'uniacid'=>$_SESSION['uniacid']));
if(!$code){
	$code=pdo_get('jy_goodserpfw_xcode',array('code'=>$security_code,'uniacid'=>$_SESSION['uniacid']));
	$code['type']=1;
}
if(!$code){
	return false;
}
$goods=pdo_get('jy_goodserpfw_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$code['goods_id']));
$juese=pdo_getcolumn('jy_goodserpfw_worker',array('juese'=>15,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'juese');
if(!$juese){
	$juese=pdo_getcolumn('jy_goodserpfw_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'juese');
}
$goods['juese']=$juese;
$codeinfo=array();
$merch=pdo_get('jy_merch_user',array('id'=>$code['merchid'],'uniacid'=>$_SESSION['uniacid']),array('merchname','desc','mobile','pcd'));
$merch['pcd']=json_decode($merch['pcd'],true);
$user_name=$merch['merchname'];
$length = mb_strlen($user_name, 'utf-8');
$firstStr = mb_substr($user_name, 0, 1, 'utf-8');
$lastStr = mb_substr($user_name, -1, 1, 'utf-8');
if($length ==1){
	$item['name']=$user_name;
}
$repeatStr = '*';
$merch['merchname']=$length == 2 ? $firstStr . str_repeat($repeatStr, $length - 1) : $firstStr . str_repeat($repeatStr, $length - 2) . $lastStr;
$mobile1=substr($merch['mobile'],0,3);
$mobile2=substr($merch['mobile'],-4);
$codeinfo['merchtitle']=$merch['merchname'];
$codeinfo['merchdesc']=$merch['desc'];
$codeinfo['merchmobile']=$mobile1.'****'.$mobile2;
$codeinfo['merchpcd']=$merch['pcd']['province'].''.$merch['pcd']['city'].''.$merch['pcd']['district'];
$codeinfo['log_statustitle']=$this->codelog_status[$code['log_status']];
$merchlog=pdo_get('jy_goodserpfw_merchlog',array('type'=>1,'code'=>$security_code,'uniacid'=>$_SESSION['uniacid']),'','','createtime desc');
$x_code=pdo_getcolumn('jy_goodserpfw_xcode',array('id'=>$code['xiang_id'],'uniacid'=>$_SESSION['uniacid']),'code');
if(!$merchlog['tomid'] && !$code['type']){
	$merchlog=pdo_get('jy_goodserpfw_merchlog',array('type'=>1,'code'=>$x_code,'uniacid'=>$_SESSION['uniacid']),'','','createtime desc');
}
if($merchlog['tomid']){
	$member=cfc('core')->member_show($merchlog['tomid']);
	$member2=pdo_get('jy_goodserpfw_members',array('mid'=>$merchlog['tomid'],'uniacid'=>$_SESSION['uniacid']));
	$member=array_merge($member,$member2);
	$codeinfo['merchtitle']=$member['nickname'];
	$codeinfo['merchdesc']=$member['cname'];
	$mobile1=substr($member['mobile'],0,3);
	$mobile2=substr($member['mobile'],-4);
	$codeinfo['merchmobile']=$mobile1.'****'.$mobile2;
	$codeinfo['merchpcd']=$member['address'];
}
if($juese==15){
	$log1=pdo_getall('jy_goodserpfw_kulog',array('code'=>$security_code,'uniacid'=>$_SESSION['uniacid']));
	$log2=pdo_getall('jy_goodserpfw_merchlog',array('code'=>$security_code,'uniacid'=>$_SESSION['uniacid']),'','','id asc');
	if(!$code['type']){
		//$log3=pdo_getall('jy_goodserpfw_kulog',array('code'=>$x_code,'uniacid'=>$_SESSION['uniacid']),'','','id asc');
		//foreach($log3 as $lv3){
			//$log2[]=$lv3;
		//}
		$log4=pdo_getall('jy_goodserpfw_merchlog',array('code'=>$x_code,'uniacid'=>$_SESSION['uniacid']),'','','id asc');
		foreach($log4 as $lv4){
			$log2[]=$lv4;
		}
		paixu($log2,'createtime','asc');
	}
	$kulog_type=$this->kulog_type;
	$merchlog_type=$this->merchlog_type;
	foreach($log1 as &$lv){
		$name=pdo_getcolumn('jy_goodserpfw_worker',array('id'=>$lv['workerid'],'uniacid'=>$_SESSION['uniacid']),'title');
		$lv['name']='仓库员['.$name.']';
		$lv['typetitle']=$kulog_type[$lv['type']];
		$lv['createtime']=date('Y-m-d H:i:s',$lv['createtime']);
	}
	foreach($log2 as &$lv){
		$name=pdo_getcolumn('jy_merch_user',array('id'=>$lv['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
		if(!$lv['tomid']){
			$lv['name']='平台出库';
			$lv['toname']='代理商['.$name.']';
		}elseif(!$lv['mid']){
			$member=cfc('core')->member_show($lv['tomid']);
			$member2=pdo_get('jy_goodserpfw_members',array('mid'=>$lv['tomid'],'uniacid'=>$_SESSION['uniacid']));
			$member=array_merge($member,$member2);
			$lv['name']='代理商['.$name.']';
			$lv['toname']='经销商['.$member['nickname'].']';
		}else{
			$member=cfc('core')->member_show($lv['mid']);
			$member2=pdo_get('jy_goodserpfw_members',array('mid'=>$lv['mid'],'uniacid'=>$_SESSION['uniacid']));
			$member=array_merge($member,$member2);
			$lv['name']='经销商['.$member['nickname'].']';
			$member2=cfc('core')->member_show($lv['tomid']);
			$lv['toname']='经销商['.$member2['nickname'].']';
		}
		$lv['typetitle']=$merchlog_type[$lv['type']];
		$lv['createtime']=date('Y-m-d H:i:s',$lv['createtime']);
	}
	$codeinfo['log1']=$log1;
	$codeinfo['log2']=$log2;
	$goods['codeinfo']=$codeinfo;
	return $goods;
}
$goods['codeinfo']=$codeinfo;
//插入查询日志
$insert=array(
	'uniacid'=>$_SESSION['uniacid'],
	'mid'=>$_SESSION['uid'],
	'goods_id'=>$code['goods_id'],
	'batch_id'=>$code['batch_id'],
	'code'=>$security_code,
	'userip'=>getip(),
	'createtime'=>TIMESTAMP,
);
pdo_insert('jy_goodserpfw_log',$insert);
return $goods;